﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using GrainManage.Base;
using GrainManage.Proxy;


namespace GrainManage.Pages
{
    /// <summary>
    /// MainPage.xaml 的交互逻辑
    /// </summary>
    public partial class MainPage : BasePage
    {

        private readonly ImageBrush TabItemDeleteIcon;

        public MainPage()
        {
            InitializeComponent();
       
            TabItemDeleteIcon = new ImageBrush(new System.Windows.Media.Imaging.BitmapImage(GetUri("Images", "delete.ico")));
        }

        private void upLoadPicture_Click(object sender, RoutedEventArgs e)
        {
            var input = new GrainManage.Entities.Image.InputBatchInsert();
            input.Authentication = App.Authentication;
            input.Images = new List<GrainManage.Entities.Table.Image>();
            var files = GetSelectedImages();
            foreach (var fileName in files)
            {
                var image = new GrainManage.Entities.Table.Image();
                image.ImageName = System.IO.Path.GetFileName(fileName);
                image.Remarks = string.Empty;
                image.File = System.IO.File.ReadAllBytes(fileName);
                image.Creator = App.Authentication.UserName;
                input.Images.Add(image);
            }
            Action action = () => Creator.ImageProxy.BatchInsert(input);
            action.BeginInvoke(null, null);
        }

        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {

        }

        private void ToolBar_Click(object sender, RoutedEventArgs e)
        {
            string pageName = (e.Source as Button).Tag as string;
            string header = (e.Source as Button).Content as string;
            NavigateTo(pageName, header);
        }

        private void NavigateTo(string pageName, string header)
        {
            BeginInvokeUI(() =>
            {
                pageName = pageName.EndsWith(".xaml") ? pageName.Replace(".xaml", string.Empty) : pageName;
                foreach (TabItem item in frames.Items)
                {
                    if (pageName == ((item.Content as Frame).Content as Page).Title)
                    {
                        item.Focus();
                        return;
                    }
                }
                var frame = new Frame();
                var pageType = Type.GetType(string.Format("GrainManage.Pages.{0}", pageName));
                frame.Content = Activator.CreateInstance(pageType);

                var sp = new StackPanel();
                //sp.Background = Brushes.YellowGreen;
                sp.Orientation = Orientation.Horizontal;
                sp.Children.Add(new TextBlock { Text = header });
                var btnDelete = new Button
                {
                    Background = TabItemDeleteIcon,
                    Width = 16,
                    Height = 16,
                    Margin = new Thickness(6, 0, -6, 0)
                };
                sp.Children.Add(btnDelete);

                var tabItem = new TabItem();
                tabItem.Header = sp;
                tabItem.Content = frame;
                //tabItem.Background = Brushes.Red;
                //tabItem.SetResourceReference(TabItem.StyleProperty, "tabStyle");
                tabItem.GotFocus += (sender, e) =>
                {
                    btnDelete.Visibility = Visibility.Visible;
                };
                tabItem.LostFocus += (sender, e) =>
                {
                    btnDelete.Visibility = Visibility.Hidden;
                };
                frames.Items.Add(tabItem);
                tabItem.Focus();

                btnDelete.Click += (sender, e) =>
                {
                    frames.Items.Remove(tabItem);
                };
            });
        }

        private string[] GetSelectedImages()
        {
            var dlg = new Microsoft.Win32.OpenFileDialog();
            dlg.Filter = AppSettings.ImageFilter;
            dlg.Multiselect = true;
            var isDisplayed = dlg.ShowDialog();
            if (isDisplayed.HasValue && isDisplayed.Value)
            {
                return dlg.FileNames;
            }
            return new string[0];
        }

        private void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
           
        }
    }
}
